import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:video_ducafecat_flutter_v3/common/index.dart';

import 'index.dart';

class MyIndexPage extends GetView<MyIndexController> {
  const MyIndexPage({Key? key}) : super(key: key);

  // 属性行
  Widget _listRow(String label, String value) {
    return <Widget>[
      TextWidget.body1(
        "$label : ",
        color: AppColors.secondary,
      ).paddingRight(AppSpace.listItem),
      TextWidget.body1(
        value,
      ),
    ].toRow().paddingBottom(AppSpace.listRow);
  }

  // 头部
  Widget _buildHeader() {
    return <Widget>[
      // 头像
      IconWidget.url(
        "${Constants.imageServer}/avatar/${UserService.to.profile.avatar}",
        size: 120,
        badgeString: UserService.to.profile.vipGrade == "guest"
            ? null
            : UserService.to.profile.vipGrade,
        // badgeColor: UserService.to.profile.vipGrade != "guest"
        //     ? AppColors.highlight
        //     : null,
        // badgeFontSize: 25,
        // isClipOval: true,
      ).paddingBottom(AppSpace.listRow),

      // 昵称
      TextWidget.title1("${UserService.to.profile.nickName}")
          .paddingBottom(AppSpace.listRow),
      // 邮件
      _listRow(
        LocaleKeys.profileHomeEmail.tr,
        "${UserService.to.profile.email}",
      ),
      // 注册日期
      _listRow(
        LocaleKeys.profileHomeRegisterDate.tr,
        UserService.to.profile.regTime!.toLocalString(),
      ),
      // 截止日期
      if (UserService.to.profile.vipGrade != null &&
          UserService.to.profile.vipGrade != "guest")
        _listRow(
          LocaleKeys.profileHomeVipDeadline.tr,
          UserService.to.profile.vipExpireTime!.toLocalString(),
        ),
      const Divider(),
    ].toColumn();
  }

  // 菜单
  Widget _menus() {
    return <Widget>[
      // // TIM 调试
      // if (kDebugMode)
      //   ListTileWidget(
      //     leading: IconWidget.icon(
      //       Icons.chat_outlined,
      //       size: 20,
      //     ),
      //     title: const TextWidget.body1("TIM 调试"),
      //     trailing: <Widget>[
      //       ButtonWidget.icon(IconWidget.icon(
      //         Icons.arrow_forward_ios,
      //         size: 15,
      //       ))
      //     ],
      //     onTap: () => Get.toNamed(RouteNames.chatChatDebug),
      //   ),

      // if (kDebugMode) const Divider(),

      // 修改邮件
      ListTileWidget(
        leading: IconWidget.icon(
          Icons.email,
          size: 20,
        ),
        title: TextWidget.body1(LocaleKeys.profileHomeMenuChangeEmail.tr),
        trailing: <Widget>[
          ButtonWidget.icon(IconWidget.icon(
            Icons.arrow_forward_ios,
            size: 15,
          ))
        ],
        onTap: controller.onChangeEmail,
      ),

      // 修改密码
      ListTileWidget(
        leading: IconWidget.icon(
          Icons.password,
          size: 20,
        ),
        title: TextWidget.body1(LocaleKeys.profileHomeMenuChangePassword.tr),
        trailing: <Widget>[
          ButtonWidget.icon(IconWidget.icon(
            Icons.arrow_forward_ios,
            size: 15,
          ))
        ],
        onTap: controller.onChangePassword,
      ),

      // 修改语言
      ListTileWidget(
        leading: IconWidget.icon(
          Icons.language,
          size: 20,
        ),
        title: TextWidget.body1(
            "${LocaleKeys.profileHomeMenuChangeLanguage.tr} - ${ConfigService.to.locale.toLanguageTag()}"),
        trailing: <Widget>[
          ButtonWidget.icon(IconWidget.icon(
            Icons.arrow_forward_ios,
            size: 15,
          ))
        ],
        onTap: controller.onLanguageSwitch,
      ),

      // 修改样式
      ListTileWidget(
        leading: IconWidget.icon(
          Icons.dark_mode,
          size: 20,
        ),
        title: TextWidget.body1(
            "${LocaleKeys.profileHomeMenuChangeTheme.tr} - ${ConfigService.to.isDarkModel ? "Dark Mode" : "Light Mode"}"),
        trailing: <Widget>[
          ButtonWidget.icon(IconWidget.icon(
            Icons.arrow_forward_ios,
            size: 15,
          ))
        ],
        onTap: () => ConfigService.to.switchThemeModel(),
      ),

      // 销毁账户
      ListTileWidget(
        leading: IconWidget.icon(
          Icons.remove_circle,
          size: 20,
        ),
        title: TextWidget.body1(
          LocaleKeys.profileHomeMenuDestory.tr,
        ),
        trailing: <Widget>[
          ButtonWidget.icon(IconWidget.icon(
            Icons.arrow_forward_ios,
            size: 15,
          ))
        ],
        onTap: () {
          ActionDialog.normal(
            context: Get.context!,
            title: Text(LocaleKeys.profileHomeMenuDestory.tr),
            content: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Text(
                  LocaleKeys.profileHomeMenuDestoryInfo.tr,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 13,
                    color: AppColors.secondary,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                const SizedBox(height: 10),
                const Text(
                  'yes',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 15,
                    color: Colors.red,
                  ),
                ),
                const SizedBox(height: 15),
                InputWidget(
                  hintText: 'Input',
                  fontSize: 14,
                  controller: controller.destroyInput,
                ),
              ],
            ),
            confirm: Text(LocaleKeys.commonBottomConfirm.tr),
            confirmBackgroundColor: Colors.red,
            onConfirm: controller.onDestroy,
          );
        },
      ),

      // 退出登录
      ListTileWidget(
        leading: IconWidget.icon(
          Icons.logout,
          size: 20,
        ),
        title: TextWidget.body1(LocaleKeys.profileHomeMenuLogout.tr),
        trailing: <Widget>[
          ButtonWidget.icon(IconWidget.icon(
            Icons.arrow_forward_ios,
            size: 15,
          ))
        ],
        onTap: controller.onLogout,
      ),
    ].toColumn();
  }

  // 主视图
  Widget _buildView() {
    return <Widget>[
      _buildHeader(),
      _menus(),
    ].toColumn().paddingAll(AppSpace.page).safeArea().scrollable();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MyIndexController>(
      // init: MyIndexController(),
      init: Get.find<MyIndexController>(),
      id: "my_index",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text(LocaleKeys.profileHomeTitle.tr)),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
